<?php

use yii\helpers\Url;

?>
<div class="layui-fluid">

    <div class="layui-card">
        <div class="layui-card-header">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-danger layuiadmin-btn-admin" onclick="delAll()">批量删除</button>
                <button class="layui-btn layuiadmin-btn-admin" onclick="categoryAdd()">添加</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-table layui-form">
                <thead>
                <tr>
                    <th width="20">
                        <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"
                             lay-filter="allChoose">
                            <i class="layui-icon">&#xe605;</i>
                        </div>
                    </th>
                    <th width="70">ID</th>
                    <th>平台名</th>

                    <th width="250">操作</th>
                </thead>
                <tbody class="x-cate">
                <?php foreach ($grade as $list): ?>
                    <tr cate-id='<?= $list['id'] ?>' fid='<?= $list['fid'] ?>'>
                        <td>
                            <div class="layui-unselect layui-form-checkbox" lay-skin="primary"
                                 data-id='<?= $list['id'] ?>' lay-filter="oneChoose"><i class="layui-icon">&#xe605;</i>
                            </div>
                        </td>
                        <td><?= $list['id'] ?></td>
                        <td>
                            <?= str_repeat($prefix, count(explode('-', $list['level']))) ?>
                            <?php if ($list['children'] == 1): ?>
                                <i class="layui-icon x-show" status='true'>&#xe623;</i>
                            <?php elseif ($list['children'] == 0&&$list['fid']==0): ?>
                                <i class="layui-icon layui-icon-triangle-d"></i>
                            <?php elseif ($list['fid'] != 0): ?>
                                ├
                            <?php endif; ?>
                            <?= $list['name'] ?>
                        </td>
                        <td class="td-manage">
                            <button class="layui-btn layui-btn layui-btn-xs"
                                    onclick="update('<?= $list['id'] ?>','<?= $list['name'] ?>')"><i
                                        class="layui-icon">&#xe642;</i>编辑
                            </button>
                            <button class="layui-btn layui-btn-warm layui-btn-xs"
                                    onclick="x_admin_show('添加','<?= Url::toRoute([$this->context->id . '/create', 'fid' => $list['id']]) ?>')">
                                <i
                                        class="layui-icon">&#xe642;</i>添加子栏目
                            </button>
                            <button class="layui-btn-danger layui-btn layui-btn-xs"
                                    onclick="member_del(this,'<?= $list['id'] ?>')"
                                    href="javascript:;"><i class="layui-icon">&#xe640;</i>删除
                            </button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

<?php $this->beginBlock('footer') ?>
<script>
    layui.config({
        base: '<?=Url::base()?>/resources/'
    }).extend({
        index: 'lib/index'
    }).use('index');
    $(".layui-form-checkbox").click(function (event) {
        if ($(this).hasClass('layui-form-checked')) {
            $(this).removeClass('layui-form-checked');
            if ($(this).hasClass('header')) {
                $(".layui-form-checkbox").removeClass('layui-form-checked');
            }
        } else {
            $(this).addClass('layui-form-checked');
            if ($(this).hasClass('header')) {
                $(".layui-form-checkbox").addClass('layui-form-checked');
            }
        }

    });
    $("tbody.x-cate tr[fid!='0']").hide();
    // 栏目多级显示效果

    $('.x-show').click(function () {
        if ($(this).attr('status') == 'true') {
            $(this).html('&#xe625;');
            $(this).attr('status', 'false');
            cateId = $(this).parents('tr').attr('cate-id');
            $("tbody tr[fid=" + cateId + "]").show();
        } else {
            cateIds = [];
            $(this).html('&#xe623;');
            $(this).attr('status', 'true');
            cateId = $(this).parents('tr').attr('cate-id');
            getCateId(cateId);
            for (var i in cateIds) {
                $("tbody tr[cate-id=" + cateIds[i] + "]").hide().find('.x-show').html('&#xe623;').attr('status', 'true');
            }
        }
    })
    var cateIds = [];

    function getCateId(cateId) {

        $("tbody tr[fid=" + cateId + "]").each(function (index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }

    /*用户-删除*/
    function member_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            //发异步删除数据
            $.get('<?=Url::toRoute([$this->context->id . '/del'])?>', {id: id}, function (res) {
                if (res.status == 200) {
                    $(obj).parents("tr").remove();
                    layer.msg(res.msg, {icon: 1, time: 1000});
                } else {
                    layer.msg(res.msg, {icon: 7, time: 1000});
                }
            }, 'json')

        });
    }

    function getData() {
        var obj = $(".layui-form-checked").not('.header');
        var arr = [];
        obj.each(function (index, el) {
            arr.push(obj.eq(index).attr('data-id'));
        });
        return arr;
    }

    function delAll() {
        var data = getData();
        if (data.length > 0) {
            layer.confirm('确认要删除吗？', function (index) {
                //捉到所有被选中的，发异步进行删除
                $.get('<?=Url::toRoute([$this->context->id . '/batch-del'])?>', {idArr: data}, function (res) {
                    if (res.status == 200) {
                        layer.msg(res.msg, {icon: 1});
                        $(".layui-form-checked").not('.header').parents('tr').remove();
                    } else {
                        layer.msg(res.msg, {icon: 7, time: 1000});
                    }
                }, 'json')
            });
        } else {
            layer.msg('请选择您要批量删除的数据', {icon: 7});
            return;
        }
    }

    function update(id, name) {
        layer.prompt({title: '请输入修改后的平台名', formType: 3, value: name}, function (text, index) {
            layer.load(3);
            $.post('<?=Url::toRoute([$this->context->id . '/edit'])?>', {
                id: id,
                name: text,
                '_csrf-backend': '<?=Yii::$app->request->csrfToken?>'
            }, function (res) {
                layer.closeAll();
                if (res.status == 200) {
                    layer.msg(res.msg, {icon: 6, time: 1500}, function () {
                        location.reload();
                    })
                } else {
                    layer.msg(res.msg, {time: 1500})
                }

            }, 'json')
        })
    }

    function categoryAdd() {
        layer.open({
            type: 2,
            area: ['420px', '420px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: '分类添加',
            content: '<?=Url::toRoute([$this->context->id . '/create'])?>'
        });
    }

</script>
<?php $this->endBlock() ?>

